#include <vector>
#include <algorithm>
#include <iostream>

using std::cout;
using std::sort;
using std::vector;

class Solution
{
public:
    vector<vector<int>> threeSum(vector<int> &&nums)
    {
        vector<vector<int>> vv;
        sort(nums.begin(), nums.end());
        int left,right,a,b,c,sum;
        for(size_t i = 0;i < nums.size(); ++ i)
        {
            if(nums[i] > 0) break;
            // 对a去重
            if(i > 0 && nums[i] == nums[i - 1]) continue;

            left = i + 1;
            right = nums.size() - 1;
            a = nums[i];
            while(1)
            {
                if(left >= right) break;
                b = nums[left];
                c = nums[right];
                sum = a + b + c;

                if(sum > 0) -- right;
                else if(sum < 0) ++ left;
                else if(sum == 0)
                {
                    vv.push_back({a,b,c});
                    // 去重b和c
                    while(left < right && nums[left] == nums[left + 1])
                        ++ left;
                    while(left < right && nums[right] == nums[right - 1])
                        -- right;
                    -- right;
                    ++ left;
                }
            }
        }
        return vv;
    }
};

int main(int argc, char const *argv[])
{
    Solution s;
    vector<vector<int>> vv = s.threeSum({0,0,0});
    for (auto &v : vv)
    {
        for (auto &num : v)
        {
            cout << num << " ";
        }
        cout << "\n";
    }
    getchar();
    return 0;
}
